﻿using System;

namespace StockholmsStad.Web.UI.UIServiceReference
{
    /// <summary>
    /// Påbyggnad på servicelagrets kontrakt för bokning.
    /// </summary>
    public partial class Booking
    {
        /// <summary>
        /// Skapar en tom bokning
        /// </summary>
        public Booking()
        {
            CeremonyType = CeremonyType.Ordinary;
        }

        ///<summary>
        /// Returnerar true om bokningen skapades innan användaren loggade in
        ///</summary>
        public bool BookingMadeBeforeLogin { get; set; }

        /// <summary>
        /// Skapar en bokning för stadshuset
        /// </summary>
        /// <param name="bookingDateTime"></param>
        /// <param name="preferredDate1"></param>
        /// <param name="preferredDate2"></param>
        /// <returns></returns>
        public static Booking createOrdinaryBooking(DateTime? bookingDateTime,
                                                    DateTime? preferredDate1, DateTime? preferredDate2)
        {
            var booking = new Booking
                              {
                                  CeremonyType = CeremonyType.Ordinary,
                                  DateTime = bookingDateTime == null
                                                 ? null
                                                 : new TimeBlock
                                                       {
                                                           StartDateTime = bookingDateTime.Value,
                                                       },
                                  PreferredDate1 = preferredDate1 ?? null,
                                  PreferredDate2 = preferredDate2 ?? null,
                                  MinisterCode = null,
                              };
            return booking;
        }

        /// <summary>
        /// Skapar en tom bokning, som default är för stadshuset
        /// </summary>
        /// <returns></returns>
        public static Booking CreateEmptyBooking()
        {
            return new Booking
                       {
                           MinisterCode = null,
                           BookingMadeBeforeLogin = false,
                           CeremonyType = CeremonyType.Ordinary,
                           DateTime = null,
                           PreferredDate1 = null,
                           PreferredDate2 = null
                       };
        }

        /// <summary>
        /// Skapar en bokning för vigsel utanför stadshuset
        /// </summary>
        /// <param name="dateText"></param>
        /// <param name="bookingCode"></param>
        /// <returns></returns>
        public static Booking createIndividualBooking(string dateText, string bookingCode)
        {
            var booking = new Booking
                              {
                                  CeremonyType = CeremonyType.Individual,
                                  DateTime = new TimeBlock
                                                 {
                                                     StartDateTime = GetDate(dateText),
                                                 },
                                  PreferredDate1 = null,
                                  PreferredDate2 = null,
                                  MinisterCode = bookingCode,
                              };
            return booking;
        }

        private static DateTime GetDate(string dateText)
        {
            return System.DateTime.ParseExact(dateText, "yyyy-MM-dd", null);
        }
    }
}